package com.hzh.cusviewdemo.Adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hzh.cusviewdemo.Bean.MyMusicData;
import com.hzh.cusviewdemo.Music.Constant;
import com.hzh.cusviewdemo.Music.MusicPlayService;
import com.hzh.cusviewdemo.Music.MyMusicUtil;
import com.hzh.cusviewdemo.R;
import com.hzh.cusviewdemo.Util.SugarUtils;

import java.util.List;

public class PlayListAdapter extends RecyclerView.Adapter<PlayListAdapter.MyHolder> {

    private List<MyMusicData> dataList;
    private Context context;

    public PlayListAdapter(List<MyMusicData> dataList, Context context) {
        this.dataList = dataList;
        this.context = context;
    }

    @NonNull
    @Override
    public PlayListAdapter.MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list, parent, false);
        return new MyHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PlayListAdapter.MyHolder holder, int position) {

        // 根据现在的播放的音乐的musicId进行
        MyMusicData data = dataList.get(position);

        if (data.getSid().equals(MyMusicUtil.getStringShared(Constant.KEY_ID))) {
            holder.ipl_icon.setVisibility(View.VISIBLE);
            holder.ipl_name.setTextColor(Color.parseColor("#1296db"));
        } else {
            holder.ipl_icon.setVisibility(View.GONE);
            holder.ipl_name.setTextColor(Color.parseColor("#000000"));
        }

        holder.ipl_name.setText(data.getName());
        holder.ipl_songer.setText("-  " + data.getSonger());

        // 点击
        holder.ipl_linear.setOnClickListener(v -> {
            // 播放，如果播放的是正在放的歌曲就不处理
//            if (data.getSid().equals(MyMusicUtil.getStringShared(Constant.KEY_ID)))
//                return;

            Intent intent = new Intent(MusicPlayService.PLAYER_MANAGER_ACTION);
            intent.putExtra(Constant.COMMAND, Constant.COMMAND_PLAY);
            intent.putExtra(Constant.KEY_PATH, data.getUrl());
            intent.putExtra(Constant.KEY_ID, data.getSid());
            intent.putExtra(Constant.KEY_TITLE, data.getTitle());
            context.sendBroadcast(intent);

            MyMusicUtil.setShared(Constant.KEY_NAME, data.getName());
            MyMusicUtil.setShared(Constant.KEY_SONGER, data.getSonger());
            MyMusicUtil.setShared(Constant.KEY_ID, data.getSid());
            notifyDataSetChanged();
        });

        // 删除
        holder.ipl_delete.setOnClickListener(v -> {
            SugarUtils.deleteMusicData(data.getSid());

            dataList.clear();
            dataList = SugarUtils.findMusicData();

            if (!data.getSid().equals(MyMusicUtil.getStringShared(Constant.KEY_ID))) {
                notifyDataSetChanged();
                return;
            }
            MyMusicUtil.setShared(Constant.KEY_ID, data.getSid());
            MyMusicUtil.setShared(Constant.KEY_NAME, data.getName());
            MyMusicUtil.setShared(Constant.KEY_SONGER, data.getSonger());

            MyMusicUtil.playNextMusic(context);
            notifyDataSetChanged();
        });
    }

    @Override
    public int getItemCount() {
        return dataList.size();
    }

    static class MyHolder extends RecyclerView.ViewHolder {

        private LinearLayout ipl_linear;
        private ImageView ipl_icon;
        private TextView ipl_name;
        private TextView ipl_songer;
        private ImageView ipl_delete;

        public MyHolder(@NonNull View itemView) {
            super(itemView);

            ipl_linear = itemView.findViewById(R.id.ipl_linear);
            ipl_icon = itemView.findViewById(R.id.ipl_icon);
            ipl_name = itemView.findViewById(R.id.ipl_name);
            ipl_songer = itemView.findViewById(R.id.ipl_songer);
            ipl_delete = itemView.findViewById(R.id.ipl_delete);
        }
    }
}
